﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace OCR.SettingsForm
{   
    public partial class TestEnvironmentForm : Form
    { 
        // 导入 C++ DLL 函数
        [DllImport(@"ocr_v4_gpu.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void CreateInstance();
        [DllImport(@"ocr_v4_gpu.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr do_ocr_Mat(IntPtr img);
        [DllImport(@"ocr_v4_gpu.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr do_ocr(byte[] imageData, int width, int height);
        [DllImport(@"ocr_v4_gpu.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void DestroyInstance();
        public TestEnvironmentForm()
        {
            InitializeComponent();
        }

        private void TestEnvironmentForm_Load(object sender, EventArgs e)
        {
            CreateInstance();

            // 读取本地图片
            string imagePath = "2025-10-16-14-21-37-1247461_NG_A_bdce46d8c11d4a04aa3b14378b45c0a8combine.jpeg"; // 替换为您的图片路径
            System.Drawing.Bitmap bitmap = new Bitmap(imagePath);

            // 获取图片数据
            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);

            int bytesPerPixel = Bitmap.GetPixelFormatSize(bitmap.PixelFormat) / 8;
            byte[] imageData = new byte[bmpData.Stride * bitmap.Height];

            // 拷贝数据
            System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, imageData, 0, imageData.Length);
            bitmap.UnlockBits(bmpData);
            IntPtr resultPtr = do_ocr(imageData, bitmap.Width, bitmap.Height);
            if (resultPtr != IntPtr.Zero) // 确保结果不为 null
            {
                string result = Marshal.PtrToStringAnsi(resultPtr);
                byte[] rebytes = Encoding.Default.GetBytes(result);
                byte[] bytes = Encoding.Convert(Encoding.UTF8, Encoding.Default, rebytes, 0, rebytes.Length);
                result = Encoding.Default.GetString(bytes, 0, bytes.Length);
                Console.WriteLine("OCR Result:\n" + result);
            }
            else
            {
                Console.WriteLine("OCR processing failed or instance was not created.");
            }
            //for (int i = 0; i < 100; i++)
            //{
            //    IntPtr resultPtr = do_ocr(imageData, bitmap.Width, bitmap.Height);
            //    if (resultPtr != IntPtr.Zero) // 确保结果不为 null
            //    {
            //        string result = Marshal.PtrToStringAnsi(resultPtr);
            //        byte[] rebytes = Encoding.Default.GetBytes(result);
            //        byte[] bytes = Encoding.Convert(Encoding.UTF8, Encoding.Default, rebytes, 0, rebytes.Length);
            //        result = Encoding.Default.GetString(bytes, 0, bytes.Length);
            //        Console.WriteLine("OCR Result:\n" + result);
            //    }
            //    else
            //    {
            //        Console.WriteLine("OCR processing failed or instance was not created.");
            //    }
            //}

            //Mat src = Cv2.ImRead(imagePath, ImreadModes.Color);
            ////Cv2.Resize(src,src, new OpenCvSharp.Size(src.Cols / 2, src.Rows / 2), (double)InterpolationFlags.Linear);
            //if (src.Empty())
            //{
            //    Console.WriteLine("无法加载图像，请检查路径是否正确。");
            //    return;
            //}

            //for (int i = 0; i < 100; i++)
            //{
            //    IntPtr resultPtr = do_ocr_Mat(src.CvPtr);
            //    if (resultPtr != IntPtr.Zero) // 确保结果不为 null
            //    {
            //        string result = Marshal.PtrToStringAnsi(resultPtr);
            //        byte[] rebytes = Encoding.Default.GetBytes(result);
            //        byte[] bytes = Encoding.Convert(Encoding.UTF8, Encoding.Default, rebytes, 0, rebytes.Length);
            //        result = Encoding.Default.GetString(bytes, 0, bytes.Length);
            //        Console.WriteLine("OCR Result:\n" + result);
            //    }
            //    else
            //    {
            //        Console.WriteLine("OCR processing failed or instance was not created.");
            //    }
            //}
        }
    }
}
